﻿using Manox.Libs.ODB;
using System;
using UnityEngine.Networking;


namespace Manox.Libs.Networking
{
    /// <summary>
    /// Unity network reader.
    /// </summary>
    public class SerializeReader : NetworkReader
    {
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="data"></param>
        public SerializeReader(byte[] data) 
            : base(data)
        {

        }

        /// <summary>
        /// Read Guid.
        /// </summary>
        /// <returns></returns>
        public Guid ReadGuid()
        {
            return new Guid(this.ReadBytesAndSize());
        }

        /// <summary>
        /// Read enum.
        /// </summary>
        /// <returns></returns>
        public object ReadEnum()
        {
            Type enumType = Type.GetType(this.ReadString());

            return Enum.Parse(enumType, this.ReadString());
        }

        /// <summary>
        /// Read ODB object.
        /// </summary>
        /// <returns></returns>
        public object ReadODBObject()
        {
            ObjectDatabaseItemProxy output = new ObjectDatabaseItemProxy();

            output.Id = this.ReadGuid();

            return output;
        }

        /// <summary>
        /// Read object.
        /// </summary>
        /// <returns></returns>
        public object ReadObject()
        {
            SerializeDataType dataType = (SerializeDataType)this.ReadInt32();

            switch (dataType)
            {
                case SerializeDataType.Null:
                    return null;
                case SerializeDataType.Int32:
                    return this.ReadInt32();
                case SerializeDataType.Single:
                    return this.ReadSingle();
                case SerializeDataType.String:
                    return this.ReadString();
                case SerializeDataType.Boolean:
                    return this.ReadBoolean();
                case SerializeDataType.Color:
                    return this.ReadColor();
                case SerializeDataType.Vector2:
                    return this.ReadVector2();
                case SerializeDataType.Vector3:
                    return this.ReadVector3();
                case SerializeDataType.Quaternion:
                    return this.ReadQuaternion();
                case SerializeDataType.Guid:
                    return this.ReadGuid();
                case SerializeDataType.Enum:
                    return this.ReadEnum();
                case SerializeDataType.ODBObject:
                    return this.ReadODBObject();
                    //case DataType.Serializable:
                    //    {
                    //        BinaryFormatter serializer = new BinaryFormatter();
                    //        return serializer.Deserialize(this);
                    //    }
            }

            return null;
        }
    }
}
